﻿using Daemon.Service.Contract;
using Daemon.Model;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using Daemon.Infrustructure.Contract;
using Daemon.Common.Exceptions;

namespace Daemon.Service
{
	public class TakeAwayUserExchangeService : ITakeAwayUserExchangeService
	{
		private readonly ITakeAwayIntegeralGoodRepository _takeAwayIntegeralGoodRepository;

		private readonly ITakeAwayAccountTransactionRepository _takeAwayAccountTransactionRepository;

		private readonly ITakeAwayUserAccountRepository _takeAwayUserAccountRepository;

		private readonly ITakeAwayUserExchangeRepository _takeAwayUserExchangeRepository;

		private readonly IUnitOfWork _unitOfWork;

		private readonly ApiDBContent _context;

		public TakeAwayUserExchangeService(ApiDBContent context, IUnitOfWork unitOfWork, ITakeAwayUserExchangeRepository takeAwayUserExchangeRepository, ITakeAwayIntegeralGoodRepository takeAwayIntegeralGoodRepository, ITakeAwayAccountTransactionRepository takeAwayAccountTransactionRepository, ITakeAwayUserAccountRepository takeAwayUserAccountRepository)
		{
			_takeAwayIntegeralGoodRepository = takeAwayIntegeralGoodRepository;
			_takeAwayAccountTransactionRepository = takeAwayAccountTransactionRepository;
			_takeAwayUserAccountRepository = takeAwayUserAccountRepository;
			_takeAwayUserExchangeRepository = takeAwayUserExchangeRepository;
			_unitOfWork = unitOfWork;
			_context = context;
		}

		public TakeAwayUserExchange Exchange(int userId, string address, int goodId, int typeId, int count, int shopId)
		{
			TakeAwayUserExchange result = null;
			var goodInfo = _takeAwayIntegeralGoodRepository.FindById(goodId);
			if (goodInfo.Qty == 0)
			{
				throw new BusinessException("领取失败！已经领完了");
			}

			var userAccountInfo = _takeAwayAccountTransactionRepository.FindAll().Where(r => r.UserId == userId).ToList();
			if (userAccountInfo.Sum(r => r.AdjustCost) < (goodInfo.Integeral * count))
			{
				throw new BusinessException("领取失败！积分不够");
			}

			var userAccount = _takeAwayUserAccountRepository.FindAll().First(r => r.UserId == userId);
			var takeAwayUserExchange = new TakeAwayUserExchange() { Address = address, AddTime = DateTime.Now, ExchangeId = goodId, Integeral = goodInfo.Integeral, TypeId = typeId, UserId = userId };
			var takeAwayAccountTransaction = new TakeAwayAccountTransaction() { AccountId = userAccount.Id, AdjustCost = goodInfo.Integeral, Description = $"用户{{0}}兑换了{count}个{goodInfo.Name},消耗了{goodInfo.Integeral * count}积分", ShopId = shopId, UserId = userId };
			try
			{
				_unitOfWork.BeginTransaction(_context);
				result = _takeAwayUserExchangeRepository.AddByDBContext(takeAwayUserExchange);
				_takeAwayAccountTransactionRepository.AddByDBContext(takeAwayAccountTransaction);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
				_unitOfWork.RollbackTransaction(_context);
				return null;
			}
			finally
			{
				_unitOfWork.CommitTransaction(_context);
			}

			return result;
		}
	}
}
